package vn.ttpcode.com.sample.keyboard;

import vn.ttpcode.com.sample.R;
import vn.ttpcode.com.sample.keyboard.KeyboardWrapperView.KeyboardListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.Toast;

public class VirtualKeyboardAct extends Activity{
	private static final String TAG = "ttpcode";
	private boolean isKeyboarkShown = false;
	private EditText etInput;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.virtual_keyboard_act);
		
		etInput = (EditText) findViewById(R.id.etInput);
	
		KeyboardWrapperView.setImplements(new KeyboardListener() {
			
			@Override
			public void onKeyboardStatusChanged(KeyEvent event, boolean hasShown) {
				// TODO Auto-generated method stub
				isKeyboarkShown = hasShown;
				dispatchKeyEvent(event);
			}
		});
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		boolean isHandle = false;
	    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
	        if (event.getAction() == KeyEvent.ACTION_DOWN
	                && event.getRepeatCount() == 0) {
	        	Log.d(TAG, "dispatchKeyEvent - dispatchKeyEvent (with keyboard status): " + isKeyboarkShown);
	        	if(isKeyboarkShown){
	        		isHandle = true;
	        	}
	        }
	    } else if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
	    	if (event.getAction() == KeyEvent.ACTION_DOWN
	                && event.getRepeatCount() == 0) {
	    		isHandle = true;
	    	}
        }
	    
	    if(isHandle){
    		Log.e(TAG, "DO SOME THING WHEN KEYBOARD CLOSED");
    		Toast.makeText(VirtualKeyboardAct.this, etInput.getText().toString(), Toast.LENGTH_SHORT).show();
	    }
	    
	    return super.dispatchKeyEvent(event);
	}

}
